package sfmc.biz.serviceImpl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;

import sfmc.biz.dao.SfmcCustomerDao;
import sfmc.biz.service.SfmcCustomerService;
import sfmc.biz.vo.Sfmc_TB_CUSTOMER;

@Service("SfmcCustomerService")
public class SfmcCustomerServiceImpl implements SfmcCustomerService{
	
	protected Log log = LogFactory.getLog(this.getClass());
	
	@Resource(name="SfmcCustomerDao")
	private SfmcCustomerDao sfmcCustomerDao;
	
	public void setSfmcCustomer(SfmcCustomerDao sfmcCustomerDao){
		this.sfmcCustomerDao = sfmcCustomerDao;
	}

	public List<Sfmc_TB_CUSTOMER> getCustomerList(Sfmc_TB_CUSTOMER sfmc_TB_CUSTOMER){
		return getCustomerList(sfmc_TB_CUSTOMER, false);
	}
	public List<Sfmc_TB_CUSTOMER> getCustomerList(Sfmc_TB_CUSTOMER sfmc_TB_CUSTOMER, boolean isExcel){
		
		String where = "";
		
		try{
			
			if(sfmc_TB_CUSTOMER.getSearchCustIdx() != 0){
				where = "AND A.custIdx = " + sfmc_TB_CUSTOMER.getSearchCustIdx() + "\n";
			}else{
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustPhone())){
					where += "AND A.custPhone LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustPhone() + "%'\n";
				}
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustName())){
					where += "AND A.custName LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustName() + "%'\n";
				}
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustCode())){
					where += "AND A.custCode LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustCode() + "%'\n";
				}
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustBoss())){
					where += "AND A.custBoss LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustBoss() + "%'\n";
				}
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustAddr())){
					where += "AND A.custAddr LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustAddr() + "%'\n";
				}
			}

			if(isExcel){
				where += "ORDER BY A.custIdx DESC \n";
			}else{
				where += "ORDER BY A.custIdx DESC LIMIT "+sfmc_TB_CUSTOMER.getOffSet()+", "+sfmc_TB_CUSTOMER.getPageSize()+" \n";
			}
			
			sfmc_TB_CUSTOMER.setWhere(where);
			
			return sfmcCustomerDao.getCustomerList(sfmc_TB_CUSTOMER);
			
		}catch(Exception e){
			log.error(e);
			return null;
		}
	}

	public int insertCustomer(Sfmc_TB_CUSTOMER dto) {
		try{
			sfmcCustomerDao.insertCustomer(dto);
			return 1;
		}catch(Exception e){
			log.error(e);
			return -1;
		}
	}
	
	public int updateCustomer(Sfmc_TB_CUSTOMER dto) {
		try{
			return sfmcCustomerDao.updateCustomer(dto);
		}catch(Exception e){
			log.error(e);
			return -1;
		}
	}

	public int deleteCustomer(Sfmc_TB_CUSTOMER dto) {
		try{
			return sfmcCustomerDao.deleteCustomer(dto);
		}catch(Exception e){
			log.error(e);
			return -1;
		}
	}

	@Override
	public int getCustomerCount(Sfmc_TB_CUSTOMER sfmc_TB_CUSTOMER) {
		String where = "";
		
		try{
			
			if(sfmc_TB_CUSTOMER.getSearchCustIdx() != 0){
				where = "AND A.custIdx = " + sfmc_TB_CUSTOMER.getSearchCustIdx() + "\n";
			}else{
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustPhone())){
					where += "AND A.custPhone LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustPhone() + "%'\n";
				}
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustName())){
					where += "AND A.custName LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustName() + "%'\n";
				}
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustCode())){
					where += "AND A.custCode LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustCode() + "%'\n";
				}
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustBoss())){
					where += "AND A.custBoss LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustBoss() + "%'\n";
				}
				if(!"".equals(sfmc_TB_CUSTOMER.getSearchCustAddr())){
					where += "AND A.custAddr LIKE '%" + sfmc_TB_CUSTOMER.getSearchCustAddr() + "%'\n";
				}
			}
			
			sfmc_TB_CUSTOMER.setWhere(where);
			
			return sfmcCustomerDao.getCustomerCount(sfmc_TB_CUSTOMER);
			
		}catch(Exception e){
			log.error(e);
			return 0;
		}
	}
}
